﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Core.Infrastructures.Cores.Models;
using Core.Services.Menu;
using Core.Web.Pages.Menu.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Core.Web.Pages.Menu
{
    public class IndexModel : PageModel
    {
        private readonly IMapper _mapper;
        private readonly IMenuService _menuService;
        public IndexModel(
            IMapper mapper,
            IMenuService menuService)
        {
            _mapper = mapper;
            _menuService = menuService;
        }

        public void OnGet()
        {

        }

        //public void OnPostList()
        //{
        //    var records = 0;
        //    var totalPage = 0;
        //    var menus = _mapper.Map<IEnumerable<MenuListViewModel>>(_menuService.GetListByPage(c => true, "Name ASC", 1, 10, ref records, ref totalPage));

        //    //return new JsonResult(new GridResult<MenuListViewModel>()
        //    //{
        //    //    Page = 1,
        //    //    Total = totalPage,
        //    //    Records = records,
        //    //    Rows = menus
        //    //});
        //}


        public IActionResult OnPostList(int pageIndex, int pageSize, string orderBy, string orderType)
        {
            var records = 0;
            var totalPage = 0;
            var menus = _mapper.Map<IEnumerable<MenuListViewModel>>(_menuService.GetListByPage(c => true, orderBy + " " + orderType, pageIndex, pageSize, ref records, ref totalPage));

            return new JsonResult(new GridResult<MenuListViewModel>()
            {
                Page = pageIndex,
                Total = totalPage,
                Records = records,
                Rows = menus
            });
        }
    }
}